package com.yict.csms.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.csms.system.dao.impl.PermissionDaoImpl;
import com.yict.csms.system.dao.impl.UserGroupPermissionDaoImpl;
import com.yict.csms.system.dao.impl.UserGroupTreeNodeDaoImpl;
import com.yict.csms.system.entity.Permission;
import com.yict.csms.system.entity.TreeNode;
import com.yict.csms.system.entity.UserGroup;
import com.yict.csms.system.entity.UserGroupPermission;
import com.yict.csms.system.entity.UserGroupTreeNode;
import com.yict.csms.system.service.IPermissionService;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class PermissionServiceImpl extends
		CommonService<UserGroupPermission, Long> implements IPermissionService {

	@Autowired
	private UserGroupPermissionDaoImpl userGrouppermissionDao;
	
	@Autowired
	private UserGroupTreeNodeDaoImpl userGroupTreeNodeDao;

	@Autowired
	private PermissionDaoImpl permissionDao;

	public List<UserGroupPermission> search(Map<String, Object> queryMap,
			PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		String field = " order by c.createDate desc";
		pageEntity = new PageEntity();
		queryString.append("from UserGroupPermission as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			if (queryMap.get("companyId") != null
					&& !"".equals(queryMap.get("companyId"))) {
				queryString.append(" and c.userName like :companyId");
			}
			if (queryMap.get("companyName") != null
					&& !"".equals(queryMap.get("companyName"))) {
				queryString.append(" and c.userCName like :userCName");
			}
			if (queryMap.get("userGroupId") != null
					&& !"".equals(queryMap.get("userGroupId"))) {
				queryString.append(" and c.userEName like :userEName");
			}
		}
		if (page.getField() != null&&!"".equals(page.getField()) &&page.getOrder()!= null && !"".equals(page.getOrder())) {
			field = " order by c.userGroup." + page.getField() + page.getOrder();
		}
		queryString.append(field);
		List<UserGroupPermission> list = userGrouppermissionDao.search(
				queryString.toString(), queryMap, page);
		pageEntity.setTotalRecord(userGrouppermissionDao.getPageEntity()
				.getTotalRecord());
		return list;
	}

	public List<Permission> findPermission() {
		StringBuilder queryString = new StringBuilder();
		queryString.append("from Permission as c order by c.createDate");
		List<Permission> list = permissionDao.search(queryString.toString(),
				null, null);
		return list;
	}

	public List<Permission> getCruPermission(Long userGroupId) {
		List<Permission> list = new ArrayList<Permission>();
		List<UserGroupPermission> li = permissionDao
				.getCruPermission(userGroupId);
		if (list != null) {
			for (UserGroupPermission permission : li) {
				list.add(permission.getPermission());
			}
		}
		return list;
	}

	@Transactional(readOnly = false)
	public boolean saveGroups(UserGroup userGroup,
			HashMap<Long, String> permissionFlag) throws Exception {
		boolean bool = false;
		try {
			Set<Entry<Long, String>> entrys = permissionFlag.entrySet();
			UserGroup Group = new UserGroup();
			userGroup.setUserGroupId(userGroup.getUserGroupId());
			for (Entry<Long, String> entry : entrys) {
				UserGroupPermission bo = new UserGroupPermission();
				Permission per = new Permission();
				per.setPermissionId(entry.getKey());
				if ("2".equals(entry.getValue())) {
					bo = userGrouppermissionDao.findPermission(
							userGroup.getUserGroupId(), entry.getKey());
				}
				bo.setPermission(per);
				bo.setUserGroup(userGroup);
				bo.setCreatePerson(userGroup.getCreatePerson());
				bo.setCreateDate(userGroup.getCreateDate());
				if ("1".equals(entry.getValue())) {
					bool = userGrouppermissionDao.save(bo);
				} else if ("2".equals(entry.getValue())) {
					bool = userGrouppermissionDao.remove(bo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}

	@Transactional(readOnly = false)
	public boolean saveMenu(UserGroup userGroup, HashMap<Long, String> treeNodeIdFlag) throws Exception {
		boolean bool = false;
		try {
			Set<Entry<Long, String>> entrys = treeNodeIdFlag.entrySet();
			userGroup.setUserGroupId(userGroup.getUserGroupId());
			userGroupTreeNodeDao.remove(userGroupTreeNodeDao.find("from UserGroupTreeNode t where t.userGroup.userGroupId = " + userGroup.getUserGroupId()));
			for (Entry<Long, String> entry : entrys) {
				UserGroupTreeNode userGroupTreeNode = new UserGroupTreeNode();
				TreeNode treeNode = new TreeNode();
				treeNode.setNodeId(entry.getKey());
				
				userGroupTreeNode.setTreeNode(treeNode);
				userGroupTreeNode.setUserGroup(userGroup);
				userGroupTreeNode.setCreatePerson(userGroup.getCreatePerson());
				userGroupTreeNode.setCreateDate(userGroup.getCreateDate());
				
				bool = userGroupTreeNodeDao.save(userGroupTreeNode);
			}
			if(entrys == null || entrys.size() <= 0) {
				bool = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}

	public boolean checkUserGroupId(Long userGroupId) {
		boolean bool = false;
		bool = userGrouppermissionDao.check(userGroupId);
		if(bool) {
			return true;
		}
		bool = userGroupTreeNodeDao.check(userGroupId);
		if(bool) {
			return true;
		}
		return false;
	}
	
	@Transactional(readOnly = false)
	public boolean savePermission(UserGroup userGroup, List<UserGroupPermission> userGroupPermissions) {
		boolean bool = false;
		try {
			userGrouppermissionDao.remove(userGrouppermissionDao.find("from UserGroupPermission t where t.userGroup.userGroupId = " + userGroup.getUserGroupId()));
			bool = userGrouppermissionDao.saveOrUpdate(userGroupPermissions);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}

}
